package cn.hfatec.shms.oa.oaLeave.service;

import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.advice.annotation.MysqlDataSourceOA;
import cn.hfatec.shms.web.WeekTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.oa.oaLeave.model.OaLeave;
import cn.hfatec.shms.oa.oaLeave.dao.OaLeaveDao;
import cn.hfatec.shms.oa.oaLeave.model.OaLeaveParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("OaLeaveService")
public class OaLeaveService {

    public static final String BEAN_ID = "OaLeaveService";

    //注入Dao层
    @Resource(name = "OaLeaveDao")
    private OaLeaveDao oaLeaveDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param oaLeaveParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSourceOA
    public Table queryOaLeaveTable(OaLeaveParams oaLeaveParams, Page page) {
     HashMap<String, Object> params = new HashMap<String, Object>();
    if (oaLeaveParams.getBeginTime()==null && oaLeaveParams.getBeginEnd()==null){
     oaLeaveParams.setBeginTime(WeekTime.getBeforeFirstMonthdate());
     oaLeaveParams.setBeginEnd(WeekTime.getBeforeLastMonthdate());}
        params.put("params", oaLeaveParams);
        return baseDao.queryTableByPage("queryOaLeave", params, page);
    }
    /**
     *
     * 查询数据
     *
     * @param oaLeaveParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSourceOA
    public Table queryOaLeaveTableSum(OaLeaveParams oaLeaveParams, Page page) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        if (oaLeaveParams.getBeginTime()==null && oaLeaveParams.getBeginEnd()==null){
            oaLeaveParams.setBeginTime(WeekTime.getBeforeFirstMonthdate());
            oaLeaveParams.setBeginEnd(WeekTime.getBeforeLastMonthdate());}
        params.put("params", oaLeaveParams);
        return baseDao.queryTableByPage("queryOaLeaveSum", params, page);
    }


    /**
     *
     * 查询数据
     *
     * @param oaLeaveParams
     * @param page
     *
     */
    @Transactional
    @MysqlDataSourceOA
    public Table getOaLeaveTableSum(OaLeaveParams oaLeaveParams, Page page) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        if (oaLeaveParams.getBeginTime()==null && oaLeaveParams.getBeginEnd()==null){
            oaLeaveParams.setBeginTime(WeekTime.getBeforeFirstMonthdate());
            oaLeaveParams.setBeginEnd(WeekTime.getBeforeLastMonthdate());}
        params.put("params", oaLeaveParams);
        return baseDao.queryTableByPage("getOaLeaveSum", params, page);
    }
    /**
     *
     * 插入数据
     * @param oaLeave
     *
     */
    @Transactional
    public void saveOaLeave(OaLeave oaLeave) {
        if(oaLeave.getId() != null) {
            OaLeave result = getOaLeave(oaLeave.getId());
            if(result != null) {
                oaLeaveDao.update(oaLeave);
            } else {
                oaLeaveDao.insert(oaLeave);
            }
        } else {
            oaLeaveDao.insert(oaLeave);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteOaLeave(Integer id) {

        oaLeaveDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param oaLeaveList
     *
     */
    @Transactional
    public void deleteOaLeaveBatch(List<String> oaLeaveList) {
        for(String id : oaLeaveList) {
            deleteOaLeave(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public OaLeave getOaLeave(Integer id) {

        return oaLeaveDao.getOaLeaveById(id);
    }
}